<template>
  <div class="random-avatar-generator">
    <div class="avatar-container">
      <h1>随机头像生成器</h1>
      <div class="avatar-wrapper">
        <img :src="currentAvatar" alt="Random Avatar" class="avatar">
        <div class="arrow arrow-left" @click="changeAvatar(-1)">
          <el-icon><ArrowLeft /></el-icon>
        </div>
        <div class="arrow arrow-right" @click="changeAvatar(1)">
          <el-icon><ArrowRight /></el-icon>
        </div>
      </div>
      <div class="button-group">
        <el-button type="primary" @click="downloadAvatar">
          <el-icon><Download /></el-icon>
          下载头像
        </el-button>
        <el-button type="warning" @click="goToHomepage">
          <el-icon><Back /></el-icon>
          返回首页
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ArrowLeft, ArrowRight, Download, Back } from '@element-plus/icons-vue'

const router = useRouter()
const avatars = ref([])
const currentIndex = ref(0)
const currentAvatar = ref('')

onMounted(() => {
  generateAvatars()
})

const generateAvatars = () => {
  avatars.value = Array.from(
    { length: 10 }, 
    () => `https://api.dicebear.com/6.x/avataaars/svg?seed=${Math.random()}`
  )
  currentAvatar.value = avatars.value[0]
}

const changeAvatar = (direction) => {
  currentIndex.value = (currentIndex.value + direction + avatars.value.length) % avatars.value.length
  currentAvatar.value = avatars.value[currentIndex.value]
}

const downloadAvatar = () => {
  fetch(currentAvatar.value)
    .then(response => response.blob())
    .then(blob => {
      const url = window.URL.createObjectURL(blob)
      const link = document.createElement('a')
      link.href = url
      link.download = 'random_avatar.svg'
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
      window.URL.revokeObjectURL(url)
      ElMessage.success('头像下载成功')
    })
    .catch(error => {
      console.error('下载头像时出错:', error)
      ElMessage.error('下载头像失败，请重试')
    })
}

const goToHomepage = () => {
  router.push('/')
}
</script>

<style lang="scss" scoped>
.random-avatar-generator {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  justify-content: center;
  align-items: center;

  .avatar-container {
    max-width: 600px;
    width: 90%;
    background-color: rgba(255, 255, 255, 0.95);
    border-radius: 20px;
    box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
    padding: 30px;
    box-sizing: border-box;
    text-align: center;

    h1 {
      color: #4a5568;
      font-size: 2.5em;
      margin: 0 0 30px 0;
      text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
    }

    .avatar-wrapper {
      position: relative;
      width: 200px;
      height: 200px;
      margin: 0 auto 30px;

      .avatar {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        object-fit: cover;
        box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        transition: transform 0.3s ease;

        &:hover {
          transform: scale(1.05);
        }
      }

      .arrow {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        font-size: 24px;
        color: #4a5568;
        background-color: rgba(255, 255, 255, 0.8);
        border-radius: 50%;
        width: 40px;
        height: 40px;
        display: flex;
        justify-content: center;
        align-items: center;
        cursor: pointer;
        transition: all 0.3s ease;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

        &:hover {
          background-color: #4a5568;
          color: white;
          transform: translateY(-50%) scale(1.1);
        }

        &.arrow-left {
          left: -50px;
        }

        &.arrow-right {
          right: -50px;
        }
      }
    }

    .button-group {
      display: flex;
      justify-content: center;
      gap: 20px;

      .el-button {
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 12px 24px;
        transition: all 0.3s ease;

        .el-icon {
          margin-right: 8px;
        }

        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }
      }
    }
  }
}

@media (max-width: 480px) {
  .random-avatar-generator {
    .avatar-container {
      width: 95%;
      padding: 20px;

      h1 {
        font-size: 24px;
        margin-bottom: 20px;
      }

      .avatar-wrapper {
        width: 150px;
        height: 150px;
        margin-bottom: 20px;

        .arrow {
          width: 30px;
          height: 30px;
          font-size: 18px;

          &.arrow-left {
            left: -35px;
          }

          &.arrow-right {
            right: -35px;
          }
        }
      }

      .button-group {
        flex-direction: column;
        gap: 10px;
      }
    }
  }
}
</style>
